//
//  ByDayEarningApplyVC.m
//  mijifen_ios
//
//  Created by mc on 2017/10/26.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByDayEarningApplyVC.h"

// cell
#import "ByCellDayEarningApply.h"

// vc
#import "ByDayEarningApplyRecordVC.h"
#import "ByDayEarningApplySuccessVC.h"

#import "ByViewMemberStoreMode.h"

@interface ByDayEarningApplyVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy)   NSArray     *sectionArray;
@property (nonatomic, copy)   NSArray     *placeArray;
@property (nonatomic, copy)   NSArray     *subTitleArray;

@property (nonatomic, strong) NSMutableDictionary *infoDic;
@end

@implementation ByDayEarningApplyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请提现";
    self.view.backgroundColor = BY_TABLE_COLOR;
    [self addLeftBackBarButtonItem];
    // 测试数据
    self.account = @"26765.23";
    [self createViews];
}

#pragma mark - getter
- (NSArray *)sectionArray{
    if (!_sectionArray) {
        _sectionArray = @[@"提现金额 *",@"提现方式 *",@"账号 *"];
    }
    return _sectionArray;
}

- (NSArray *)placeArray{
    if (!_placeArray) {
        _placeArray = @[@"请输入提现金额",@"请选择提现方式",@"请输入账号"];
    }
    return _placeArray;
}

- (NSArray *)subTitleArray{
    if (!_subTitleArray) {
        _subTitleArray = @[@"全部提现",@"Conjuration_arrow_down",@""];
    }
    return _subTitleArray;
}

-(NSMutableDictionary *)infoDic{
    if (!_infoDic) {
        _infoDic = [NSMutableDictionary dictionary];
    }
    return _infoDic;
}
#pragma mark -
- (void)createViews{
    UIView *headerView = [[UIView alloc] init];
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        }else{
            make.top.equalTo(self.view.mas_top);
        }
        make.left.equalTo(self.view.mas_left);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 70));
    }];
    headerView.backgroundColor = BY_THEME_COLOR;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top).offset(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 30));
    }];
    titleLabel.text = [NSString stringWithFormat:@"账户余额：¥%@",self.account];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT(24);
    titleLabel.textColor = [UIColor whiteColor];
    
    // 申请提现
    UIButton *applyButton = [[UIButton alloc] init];
    [self.view addSubview:applyButton];
    [applyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.bottom.equalTo(self.view.mas_bottom);
        }
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
    }];
    [applyButton setTitle:@"申请提现" forState:UIControlStateNormal];
    [applyButton setTitleColor:BY_TEXT_COLOR forState:UIControlStateNormal];
    [applyButton setBackgroundColor:BY_THEME_COLOR];
    [applyButton addTarget:self action:@selector(applySuccess) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *alertLabel = [[UILabel alloc] init];
    [self.view addSubview:alertLabel];
    NSString *alertInfo = @"注：微信、支付宝2小时内到账。\
                           \n  银行转账当天到账，实际到账时间取决于银行时间";
    alertLabel.numberOfLines = 0;
    CGFloat height = [alertInfo getRectOfStringWithSize:CGSizeMake(SCREEN_WIDTH - 40, MAXFLOAT) font:16].size.height;
    [alertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(20));
        make.bottom.equalTo(applyButton.mas_top).offset(-40);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 40, height));
    }];
    alertLabel.text = alertInfo;
    alertLabel.font = FONT(16);
    alertLabel.textColor = [UIColor redColor];
    
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(@(0));
        make.bottom.equalTo(alertLabel.mas_top).offset(-40);
    }];
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    

    
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sectionArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellDayEarningApply *cell = [ByCellDayEarningApply cellWithTableView:tableView andPlaceTitle:self.placeArray[indexPath.section] andSubTitle:self.subTitleArray[indexPath.section]];
    __weak typeof(self) weakSelf = self;
    __weak typeof(cell) weakCell = cell;
    cell.allApplyBlock = ^{
        weakCell.textField.text = weakSelf.account;
    };
    cell.applyMethodBlock = ^{
        ByViewMemberStoreMode *modeView = [[ByViewMemberStoreMode alloc] init];
        modeView.modeArray = @[@"微信",@"支付宝",@"银行"];
        modeView.didSelectBlock = ^(NSString *str) {
            weakCell.textField.text = str;
            [weakSelf.infoDic setValue:str forKey:[NSString stringWithFormat:@"%ld",indexPath.section + 10]];
        };
        [modeView show];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"hideApplyKeyboard" object:nil];
    };
    cell.editBlock = ^(NSString *str) {
        [weakSelf.infoDic setValue:str forKey:[NSString stringWithFormat:@"%ld",indexPath.section + 10]];
    };
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 20)];;
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:self.sectionArray[section]];
    [text addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(text.length - 1, 1)];
    sectionLabel.attributedText = text;
    sectionLabel.font = FONT(16);
    headerView.backgroundColor = BY_TABLE_COLOR;
    [headerView addSubview:sectionLabel];
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 2) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        CGFloat width = [@"提现记录 >>" getSizeOfString:16].width + 10;
        UIButton *applyRecord = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - width - 10, 20, width, 30)];
        [footerView addSubview:applyRecord];
        [applyRecord setTitle:@"提现记录 >>" forState:UIControlStateNormal];
        [applyRecord setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        applyRecord.titleLabel.font = FONT(16);
        [footerView addSubview:applyRecord];
        [applyRecord addTarget:self action:@selector(applyRecord) forControlEvents:UIControlEventTouchUpInside];
        return footerView;
    }
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 2) {
        return 50;
    }
    return 0.0001;
}


#pragma mark - 提现记录
- (void)applyRecord{
    ByDayEarningApplyRecordVC *vc = [[ByDayEarningApplyRecordVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}

#pragma mark - 申请成功
- (void)applySuccess{
    NSString *price = self.infoDic[@"10"];
    if (price.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入提现金额"];
        return;
    }
    NSString *type = self.infoDic[@"11"];
    if (type.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请选择提现方式"];
        return;
    }
    NSString *account = self.infoDic[@"12"];
    if (account.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入账号"];
        return;
    }
    ByDayEarningApplySuccessVC *vc = [[ByDayEarningApplySuccessVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}


@end
